import {
    getHistoryOrder,
    batchAddShopping
} from "../../utils/config"
import store from "../../utils/factory"
const util = require('../../utils/util.js')

const app = getApp()

let that = null;

Page({
    data: {
        detail: {},
        quantity: 0,
        remark: "",
        unitPrice: 0,
        total: 0,
        allcheck: true,
        list: [],
        showtab: 0,
        currentDate: null,
        tabitem: []
    },
    onLoad: function () {
        that = this
    },
    onShow() {
        this.getNowDate()
    },
    setTab(e) {
        let index = e.currentTarget.dataset.index;
        let date = e.currentTarget.dataset.date;
        this.setData({
            showtab: index,
            currentDate: date
        })
        this.getHistoryOrder(date)
    },
    submitOrder() {
        if (this.data.submitStatus) {
            return false
        }
        this.setData({
            submitStatus: true
        })
        wx.showLoading({
            title: "提交中"
        })
        let items = [];
        this.data.detail.map(item => {
            if (item.check) {
                items.push({
                    category: item.deliveryCategory.id,
                    quantity: 1
                })
            }
        })
        batchAddShopping({
            items: items
        }).then(res => {
            wx.hideLoading()
            that.setData({
                submitStatus: false
            })
            wx.showModal({
                title: "温馨提示",
                content: "一键加入购物车成功",
                showCancel: false, //是否显示取消按钮
                success: function (res) {
                    wx.navigateBack({
                        delta: 1
                    });
                }
            });
        }).catch(err => {
            wx.hideLoading()
        })
    },
    allCheckFn() {
        let list = this.data.detail;
        this.setData({
            allcheck: !this.data.allcheck
        })
        if (this.data.allcheck) {
            list.map(item => {
                item.check = true
            })
        } else {
            list.map(item => {
                item.check = false
            })
        }
        this.setData({
            detail: list
        })
    },
    // 减去
    bindMinus(e) {
        let index = e.currentTarget.dataset.index;
        let shopCarList = this.data.detail;
        // if (this.data.quantity == 1) {
        //   wx.showToast({
        //     icon: 'none',
        //     title: "商品数量最少不小于1"
        //   })
        //   return false
        // }
        // let quantity = util.accAdd(this.data.quantity, -1);
        // let a = this.data.unitPrice;
        // let total = util.accAdd(this.data.total, -a)
        // this.setData({
        //   quantity: quantity,
        //   total: total
        // })
    },
    // 输入
    bindManual(e) {
        // if (e.detail.value == 0) {
        //   wx.showToast({
        //     icon: 'none',
        //     title: "商品数量最少不小于1"
        //   })
        //   this.setData({
        //     quantity: 1,
        //     total: this.data.unitPrice
        //   })
        //   return false
        // }
        // this.setData({
        //   quantity: e.detail.value,
        //   total: util.accMul(e.detail.value, this.data.unitPrice)
        // })
    },
    // 加法
    bindPlus(e) {
        // let quantity = util.accAdd(this.data.quantity, 1);
        // let total = util.accAdd(this.data.total, this.data.unitPrice)
        // this.setData({
        //   quantity: quantity,
        //   total: total
        // })
    },
    check(e) {
        let index = e.currentTarget.dataset.index;
        let shopCarList = this.data.detail;
        shopCarList[index].check = !shopCarList[index].check
        if (!shopCarList[index].check) {
            this.setData({
                detail: shopCarList,
                allcheck: false
            })
        } else {
            let ret2 = shopCarList.find((v) => {
                return v.check === false;
            });
            console.log(ret2)
            if (!ret2) {
                this.setData({
                    detail: shopCarList,
                    allcheck: true
                })
            } else {
                this.setData({
                    detail: shopCarList,
                    allcheck: false
                })
            }
        }
    },
    watchRemark(e) {
        this.setData({
            remark: e.detail.value
        })
    },
    getNowDate() {
        var list = [];
        var date = new Date();
        var y = date.getFullYear();
        var m = date.getMonth() + 1; //获取当前月份的日期 
        if (m < 10) {
            m = '0' + m;
        };
        for (let i = 1; i < 8; i++) {
            var d = date.getDate()
            d = d - i;
            if (d < 10) {
                d = '0' + d;
            };
            list.push({
                date: `${y}-${m}-${d}`,
                text: `${m}.${d}`
            })
        }
        this.setData({
            tabitem: list
        });
        this.getHistoryOrder(list[0].date)
    },
    getHistoryOrder(date) {
        wx.showLoading({
            title: "加载中..."
        })
        this.setData({
            detail: []
        })
        getHistoryOrder({
            date: date
        }).then(res => {
            let req = res.rows;
            req.map(item => {
                item.check = true
            })
            console.log(req)
            this.setData({
                detail: req
            })
            wx.hideLoading()
        }).catch(err => {
            wx.hideLoading()
        })
    }
})